package util;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import dto.CandidateDTO;
import dto.CandidateViewDto;
import dto.InterviewViewDto;
import dto.RoomDTO;
import dto.TemplateViewDto;
import entities.Candidate;
import entities.Interview;


/**
 * Session Bean implementation class TesterViewConversion
 */
@Stateless
public class TesterViewConversion implements TesterViewConversionRemote {
	@PersistenceContext
	EntityManager em;
	Boolean taken;
	

	/**
	 * Default constructor.
	 */
	public TesterViewConversion() {
		// TODO Auto-generated constructor stub
	}

	public CandidateViewDto fromCandidateViewDto(Candidate candidate)
			throws Exception { // convert to CandidateViewDto
		CandidateViewDto cand = new CandidateViewDto();
		try {
			cand.setId(candidate.getId());
			cand.setName(candidate.getFirstName() + " "
					+ candidate.getLastName()); // set the name to type:
												// Firtsname Lastname
			cand.setInterviews(getInterviews(candidate.getInterviews()));
			cand.setEmail(candidate.getEmail());
		} catch (Exception e) {
			e.printStackTrace();
			//throw new Exception(
			//		"Couldn't convert from CandidateDto to Candidate", e);
		}
		return cand;
	}

	public List<InterviewViewDto> getInterviews(List<Interview> inter)
			throws Exception { // convert each interview of the candidate
		List<InterviewViewDto> interviews = new ArrayList<InterviewViewDto>();
		String accept;
		try {
			for (Interview i : inter) {
				InterviewViewDto in = new InterviewViewDto();
				in.setId(i.getId());
				if (i.getAccepted() == -1)
					accept = "N/A"; // if the accepted property was not set, it
									// will be displayed as Not Available
				else if (i.getAccepted() == 1)
					accept = "Yes"; // if the accepted field is 1, the candidate
									// has accepted the interview
				else
					accept = "No"; // otherwise, he/she has not accepted it
				in.setAccepted(accept);
				
				TemplateViewDto template = new TemplateViewDto();
				try{
				template.setTitle(i.getTemplate().getTitle());
				in.setDate(i.getDate().toString());
				in.setStartTime(i.getStartTime().toString());
				RoomDTO room= new RoomDTO();
				room.setAddress(i.getRoom().getAddress());
				in.setRoom(room);
				in.setEndTime(i.getEndTime().toString());
				}catch (Exception e){
				template.setTitle("You have to add and send a new Interview!");
				in.setDate("N/A");
				in.setStartTime("N/A");
				RoomDTO room= new RoomDTO();
				room.setAddress("N/A");
				in.setRoom(room);
				in.setEndTime("N/A");
				}
				in.setTemplate(template);
				
				CandidateDTO candidate= new CandidateDTO();
				candidate.setId(i.getCandidate().getId());
				candidate.setEmail(i.getCandidate().getEmail());
				candidate.setFirstName(i.getCandidate().getFirstName());
				candidate.setLastName(i.getCandidate().getLastName());
				in.setCandidate(candidate);
				if (i.getTestQuestions().size()>0)
					taken= true;
				else taken=false;
				in.setTaken(taken);
				interviews.add(in);

			}
		} catch (Exception e) {
			e.printStackTrace();
			//throw new Exception(
			//		"Couldn't convert from InterviewViewDto to Interview", e);
		}
		return interviews;
	}

}
